package com.soft.javase.day12.bank;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/5/19
 */
public class Account {
    private double balance;
    private final Lock lock = new ReentrantLock();

    public Account(double balance) {
        this.balance = balance;
    }

    public boolean withdraw(double money) {
        lock.lock();
        try {
            if (balance <= 0) {
                System.out.println(Thread.currentThread().getName() + " 尝试取款，但账户余额为0");
                return false;
            }

            if (money > balance) {
                System.out.println(Thread.currentThread().getName() + " 尝试取款" + money
                        + "，但余额不足，当前余额: " + balance);
                return false;
            }

            balance -= money;
            System.out.println(Thread.currentThread().getName() + " 成功取款: " + money
                    + "，剩余余额: " + balance);
            return true;
        } finally {
            lock.unlock();
        }
    }
}
